# coding: utf-8

from selenium.webdriver.support.events import (AbstractEventListener, EventFiringWebDriver)

from spider import logger


class SeleniumListener(AbstractEventListener):

    def __init__(self):
        pass

    def register(self, driver):
        return EventFiringWebDriver(driver, self)

    def before_navigate_to(self, url, driver):
        logger.debug('ready navigate url:{0}'.format(url))
        super(SeleniumListener, self).before_navigate_to(url, driver)

    def after_navigate_to(self, url, driver):
        logger.debug('navigate url:{0}'.format(url))
        super(SeleniumListener, self).after_navigate_to(url, driver)

    def before_navigate_back(self, driver):
        logger.debug('ready navigate back')
        super(SeleniumListener, self).before_navigate_back(driver)

    def after_navigate_back(self, driver):
        logger.debug('navigate back')
        super(SeleniumListener, self).after_navigate_back(driver)

    def before_navigate_forward(self, driver):
        logger.debug('ready navigate forward')
        super(SeleniumListener, self).before_navigate_forward(driver)

    def after_navigate_forward(self, driver):
        logger.debug('navigate forward')
        super(SeleniumListener, self).after_navigate_forward(driver)

    def before_find(self, by, value, driver):
        logger.debug('ready find object. func:{0}, value:{1}'.format(by, value))
        super(SeleniumListener, self).before_find(by, value, driver)

    def after_find(self, by, value, driver):
        logger.debug('find object. func:{0}, value:{1}'.format(by, value))
        super(SeleniumListener, self).after_find(by, value, driver)

    def before_click(self, element, driver):
        logger.debug('ready click object. object:{0}'.format(element))
        super(SeleniumListener, self).before_click(element, driver)

    def after_click(self, element, driver):
        logger.debug('click object. object:{0}'.format(element))
        super(SeleniumListener, self).after_click(element, driver)

    def before_change_value_of(self, element, driver):
        logger.debug('ready change. element:{0}, value:{1}'.format(element.tag_name, element.get_attribute('value')))
        super(SeleniumListener, self).before_change_value_of(element, driver)

    def after_change_value_of(self, element, driver):
        logger.debug('change. element:{0}, value:{1}'.format(element.tag_name, element.get_attribute('value')))
        super(SeleniumListener, self).after_change_value_of(element, driver)

    def before_execute_script(self, script, driver):
        logger.debug('ready run script:{0}'.format(script))
        super(SeleniumListener, self).before_execute_script(script, driver)

    def after_execute_script(self, script, driver):
        logger.debug('run script:{0}'.format(script))
        super(SeleniumListener, self).after_execute_script(script, driver)

    def before_close(self, driver):
        logger.debug('ready close.')
        super(SeleniumListener, self).before_close(driver)

    def after_close(self, driver):
        logger.debug('close.')
        super(SeleniumListener, self).after_close(driver)

    def before_quit(self, driver):
        logger.debug('ready quit.')
        super(SeleniumListener, self).before_quit(driver)

    def after_quit(self, driver):
        logger.debug('quit.')
        super(SeleniumListener, self).after_quit(driver)

    def on_exception(self, exception, driver):
        logger.debug('exception:{0}'.format(exception))
        super(SeleniumListener, self).on_exception(exception, driver)
